package event

import (
	"context"
	"ez/apps/mes/auto/mc"
	mes "ez/apps/mes/document"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
)

func init() {
	ez.Subscribe(mc.PaperEventAfterSave, func(v interface{}, ctx context.Context) {
		//md ,ok := v.(*mes.Paper)

	})
	ez.Subscribe(mc.PaperLinksEventAfterSave, func(v interface{}, ctx context.Context) {
		md, ok := v.(*mes.PaperLinks)
		if ok {
			crud := mes.NewPaperLinksCrud()
			list, _ := crud.FindBy(ss.M{"srcPaperId": md.SrcPaperId}, nil, 0, 0)
			sum := 0.0
			for _, links := range list {
				sum += links.NumUse
			}
			md.LoadSrcPaper()
			md.SrcPaper.NumUsed = sum
			_ = md.SrcPaper.Save()
		}
	})
}
